<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_User_entity{
	var $current_dbprefix = "";
	var $id = 0;
	var $username ="";
	var $email = "";
	var $name = "";
	var $password = "";
	var $permission_slug = "";
	var $address = "";
	var $phone = "";
	var $created_time = 0;
	var $commission = 0;
	var $country_id = "";
	var $site_ref = "";
	var $state_id = "";
	var $province_id = "";
	var $city_id = "";
	var $activated_code = "";
	var $status = "";
	var $email_changed = "";
	var $logo = '';
	var $escorts_apply_number = 0;
	var $borncountry_id = 0;
	
	function __construct() {
		$ci= & get_instance();
		$this->current_dbprefix = $ci->db->dbprefix;
		$ci->db->set_dbprefix('default_');
	}
	function __destruct() {
		$ci= & get_instance();
		$ci->db->set_dbprefix($this->current_dbprefix);
	}
    
	function init($cmd, $input)
	{
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		if($cmd == 'id')
		{
			$result = $ci->db->where('id',$input)->get(TBL_USER)->result();
		}
		else if($cmd == 'username')
		{
			$result = $ci->db->where('username',$input)->get(TBL_USER)->result();
		}
		else if($cmd == 'email')
		{
			$result = $ci->db->where('email',$input)->get(TBL_USER)->result();
		}
		else if($cmd == 'activated_code')
		{
			$result = $ci->db->where('activated_code',$input)->get(TBL_USER)->result();
		}
		else
		{
			return false;
		}
		
		if( !empty($result) )
		{
			$this->id = $result[0]->id;
			$this->username = $result[0]->username;
			$this->email = $result[0]->email;
			$this->password = $result[0]->password;
			$this->name = $result[0]->name;
			$this->permission_slug = $result[0]->permission_slug;
			$this->address = $result[0]->address;
			$this->phone = $result[0]->phone;
			$this->created_time = $result[0]->created_time;
			$this->commission = $result[0]->commission;
			$this->country_id = $result[0]->country_id;
			$this->site_ref = $result[0]->site_ref;
			$this->state_id = $result[0]->state_id;
			$this->province_id = $result[0]->province_id;
			$this->city_id = $result[0]->city_id;
			$this->activated_code = $result[0]->activated_code;
		 	$this->status = $result[0]->status;
			$this->email_changed = $result[0]->email_changed;
		 	$this->logo = $result[0]->logo;
			$this->escorts_apply_number = $result[0]->escorts_apply_number;
		 	$this->borncountry_id = $result[0]->borncountry_id;
			
			// Create user folder if not exists
			$directory = "./uploads/default/users";
			if(!file_exists($directory)){
				mkdir($directory);
				chmod( $directory, 0777 ) ;
				file_put_contents($directory."/index.php","");
				file_put_contents($directory."/index.html","");
			}
			
			$directory = "./uploads/default/users/".$result[0]->id ;
			if (!file_exists ( $directory )) {
				mkdir($directory);
				chmod( $directory, 0777 ) ;
				file_put_contents($directory."/index.php","");
				file_put_contents($directory."/index.html","");
			}
			return $this;
		}
		return false;
	}
	
	function setUserName($input){ $this->username=$input; }
	function setEmail($input){ $this->email=$input; }
	function setPassword($input){ $this->password=$input; }
	function setName($input){ $this->name=$input; }
	function setPermissionSlug($input){ $this->permission_slug=$input; }
	function setAddress($input){ $this->address=$input; }
	function setPhone($input){ $this->phone=$input; }
	function setCreateTime($input){ $this->created_time=$input; }
	function setCommission($input){ $this->commission=$input; }
	function setCountryId($input){ $this->country_id=$input; }
	function setSiteRef($input){ $this->site_ref=$input; }
	function setStateId($input){ $this->state_id=$input; }
	function setProvinceId($input){ $this->province_id=$input; }
	function setCityId($input){ $this->city_id=$input; }
	function setActivatedCode($input){ $this->activated_code=$input; }
	function setStatus($input){ $this->status=$input; }
	function setEmailChanged($input){ $this->email_changed=$input; }
	function setLogo($input){$this->logo=$input;}
	function setEscortsApplyNumber($input){$this->escorts_apply_number=$input;}
	function setBornCountryId($input){$this->borncountry_id=$input;}
	
	function getId(){ return $this->id; }
	function getUserName(){return $this->username; }
	function getEmail(){return $this->email; }
	function getPassword(){return $this->password; }
	function getName(){return $this->name; }
	function getPermissionSlug(){return $this->permission_slug; }
	function getAddress(){return $this->address; }
	function getPhone(){return $this->phone; }
	function getCreateTime(){return $this->created_time; }
	function getCommission(){return $this->commission; }
	function getCountryId(){return $this->country_id; }
	function getSiteRef(){return $this->site_ref; }
	function getStateId(){return $this->state_id; }
	function getProvinceId(){return $this->province_id; }
	function getCityId(){return $this->city_id; }
	function getActivatedCode(){return $this->activated_code; }
	function getStatus(){return $this->status; }
	function getEmailChanged(){return $this->email_changed; }
	function getLogo(){return $this->logo;}
	function getEscortsApplyNumber(){return $this->escorts_apply_number;}
	function getBornCountryId(){return $this->borncountry_id;}
	
	function getFolderFileStorage(){ 
		return "./uploads/default/users/".$this->id."/"; 
	}
	
	function getLogoUser(){
		return $this->getFolderFileStorage().$this->getLogo();
	}
	 
	function checkPassword($string)
	{
		if($string == $this->password)
		{
			return true;
		}
		return false;
	}
	
	function update()
	{
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		if($this->password != "")
		{
			$ci->db->set('password',$this->password);
		}
		$ci->db->set('email', $this->email);
		$ci->db->set('username', $this->username);
		$ci->db->set('name', $this->name);
		$ci->db->set('permission_slug', $this->permission_slug);
		$ci->db->set('address', $this->address);
		$ci->db->set('created_time', $this->created_time);
		$ci->db->set('commission', $this->commission);
		$ci->db->set('country_id', $this->country_id);
		$ci->db->set('phone', $this->phone);
		$ci->db->set('site_ref', $this->site_ref);
		$ci->db->set('state_id', $this->state_id);
		$ci->db->set('province_id', $this->province_id);
		$ci->db->set('city_id', $this->city_id);
		$ci->db->set('activated_code', $this->activated_code);	
		$ci->db->set('status', $this->status);
		$ci->db->set('email_changed', $this->email_changed);
		$ci->db->set('logo', $this->logo);
		$ci->db->set('escorts_apply_number',$this->escorts_apply_number);
		$ci->db->set('borncountry_id',$this->borncountry_id);
		
		$ci->db->where('id', $this->id)->update(TBL_USER);
	}
	
	function createNewUserEntity(){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		$ci->db->set('activated_code', $this->generateActivatedCode() );
		$ci->db->insert(TBL_USER);
		
		return $ci->db->insert_id();
	}
	
	function generateActivatedCode( ){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		$records = $ci->db->get(TBL_USER)->result();
		$numberArray = array();
		foreach($records as $obj){
			$numberArray[] = $obj->activated_code;
		}
		while(true){
			$rand = $ci->digit->rand_string(20);
			if(!in_array($rand, $numberArray)){
				return $rand;
			}
		}
	}	
	
	function cleanFiles($escort_id){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		$usingFilesArray= array();
		$arrayFetch = $ci->db->where('escort_id',$escort_id)->get(TBL_MEDIA)->result();
		
		$ESCORT = $ci->escort_entity->init('id',$escort_id);
		
		foreach($arrayFetch as $item){
			if($item->name){
				$usingFilesArray[] = $item->name;
			}
			if($item->avatar){
				$usingFilesArray[] = $item->avatar;
			}
			if($item->public_top_image_big){
				$usingFilesArray[] = $item->public_top_image_big;
			}
			if($item->public_top_image_thin){
				$usingFilesArray[] = $item->public_top_image_thin;
			}
			if($item->public_image){
				$usingFilesArray[] = $item->public_image;
			}
		}
		
		$usingFilesArray= array_filter($usingFilesArray);
		if ($handle = opendir("./uploads/default/users/".$ESCORT->getUserId()."/")) {
			while (false !== ($file = readdir($handle))) {
				if(!in_array($file, $usingFilesArray) AND $file !='.' AND $file != '..' AND $file != 'index.php' AND $file != 'index.html'){
					@unlink("./uploads/default/users/".$ESCORT->getUserId()."/".$file);
				}
			}
			closedir($handle);
		}
	}
	function getAllUser(){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		$result = $ci->db->order_by('id','desc')->get(TBL_USER)->result();
		return $result;
	}
	function getUserListByCountry($countryIdList){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		$ci->db->where_in('country_id', $countryIdList);
		$result = $ci->db->order_by('id','desc')->get(TBL_USER)->result();
		return $result;
	}
	
	function getUserListByType($limit,$permissionSlugArr, $countryArr = array(), $stateArray = array(), $provinceArray = array(), $cityArray = array()){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		if(count($permissionSlugArr) > 0){
			$ci->db->where_in('permission_slug', $permissionSlugArr);	
		}
		if(count($countryArr) > 0){
			$ci->db->where_in('country_id', $countryArr);
		}
		if(count($stateArray) > 0){
			$ci->db->where_in('state_id', $stateArray);
		}
		if(count($stateArray) > 0){
			$ci->db->where_in('province_id', $provinceArray);
		}
		if(count($stateArray) > 0){
			$ci->db->where_in('city_id', $cityArray);
		}
		if(count($limit) >0){
			$result = $ci->db->limit($limit[0], $limit[1]);
		}
		$result = $ci->db->order_by('id','desc')->get(TBL_USER)->result();
		return $result;
	}
	
	function countUserListByType($permissionSlugArr, $countryArr = array(), $stateArray = array(), $provinceArray = array(), $cityArray = array()){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		if(count($permissionSlugArr) > 0){
			$ci->db->where_in('permission_slug', $permissionSlugArr);	
		}
		if(count($countryArr) > 0){
			$ci->db->where_in('country_id', $countryArr);
		}
		if(count($stateArray) > 0){
			$ci->db->where_in('state_id', $stateArray);
		}
		if(count($stateArray) > 0){
			$ci->db->where_in('province_id', $provinceArray);
		}
		if(count($stateArray) > 0){
			$ci->db->where_in('city_id', $cityArray);
		}
		return $ci->db->count_all_results(TBL_USER);
	}
	
	public function getEscortIdListByAdmin($userName, $groupName, $zoneList = array(), $escortName = ''){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		if($escortName != ''){
			$ci->db->like('escort_name', $escortName);
		}
		$idList = array();
		if($groupName == USER_ADMIN){
			$escortList = $ci->db->order_by('user_id','desc')->get(TBL_ESCORT)->result();
			if($escortList){
				foreach ($escortList as $escort){
					$idList[] = $escort->id;
				}
			}
		}else if($groupName == USER_SUPPORT_LEVEL_1){
			$escortList = $ci->db->order_by('user_id','desc')->get(TBL_ESCORT)->result();
			if($escortList){
				foreach ($escortList as $escort){
					$idList[] = $escort->id;
				}
			}
		}else if($groupName == USER_TYPE_QUALITY_CONTROL){
			$escortList = $ci->db->order_by('user_id','desc')->get(TBL_ESCORT)->result();
			if($escortList){
				foreach ($escortList as $escort){
					$idList[] = $escort->id;
				}
			}
		}else if($groupName == USER_COUNTRY_MANAGER){
			$escortList = array();
			if($zoneList){
				foreach ($zoneList as $zone){
					if($zone->country_id == 0){
						$escortList = $ci->db->order_by('user_id','desc')->get(TBL_ESCORT)->result();
						break;
					}else{
						$escortListTemp = $ci->db->where('country_id', $zone->country_id)->order_by('user_id','desc')->get(TBL_ESCORT)->result();
						$escortList = $escortList +$escortListTemp;
					}
				}
			}
			if($escortList){
				foreach ($escortList as $escort){
					$idList[] = $escort->id;
				}
			}
		}else if($groupName == USER_PROVINCE_MANAGER){
			$escortList = array();
			if($zoneList){
				foreach ($zoneList as $zone){
					if($zone->country_id == 0){
						$escortList = $ci->db->order_by('user_id','desc')->get(TBL_ESCORT)->result();
						break;
					}else if($zone->state_id == 0){
						$escortListTemp = $ci->db->where('country_id', $zone->country_id)->order_by('user_id','desc')->get(TBL_ESCORT)->result();
						$escortList = $escortList +$escortListTemp;
					}else{
						if($zone->province_id == 0){
							$escortListTemp = $ci->db->where(array('country_id'=> $zone->country_id, 'state_id'=> $zone->state_id))->order_by('user_id','desc')->get(TBL_ESCORT)->result();
						}else{
							$escortListTemp = $ci->db->where(array('country_id'=> $zone->country_id, 'state_id' =>$zone->state_id, 'province_id' => $zone->province_id))->order_by('user_id','desc')->get(TBL_ESCORT)->result();
						}
						$escortList = $escortList +$escortListTemp;
					}
				}
			}
			if($escortList){
				foreach ($escortList as $escort){
					$idList[] = $escort->id;
				}
			}
		}else if($groupName == USER_CITY_MANAGER){
			if($zoneList){
				foreach ($zoneList as $zone){
					if($zone->country_id == 0){
						$escortList = $ci->db->order_by('user_id','desc')->get(TBL_ESCORT)->result();
						break;
					}else if($zone->state_id == 0){
						$escortListTemp = $ci->db->where('country_id', $zone->country_id)->order_by('user_id','desc')->get(TBL_ESCORT)->result();
						$escortList = $escortList +$escortListTemp;
					}else{
						if($zone->province_id == 0){
							if($zone->city_id == 0){
								$escortListTemp = $ci->db->where(array('country_id'=> $zone->country_id, 'state_id'=> $zone->state_id))->order_by('user_id','desc')->get(TBL_ESCORT)->result();
							}else{
								$escortListTemp = $ci->db->where(array('country_id'=> $zone->country_id, 'state_id'=> $zone->state_id, 'city_id' => $zone->city_id))->order_by('user_id','desc')->get(TBL_ESCORT)->result();
							}
						}else{
							if($zone->city_id == 0){
								$escortListTemp = $ci->db->where(array('country_id'=> $zone->country_id, 'state_id' =>$zone->state_id, 'province_id' => $zone->province_id))->order_by('user_id','desc')->get(TBL_ESCORT)->result();
							}else{
								$escortListTemp = $ci->db->where(array('country_id'=> $zone->country_id, 'state_id' =>$zone->state_id, 'province_id' => $zone->province_id, 'city_id' => $zone->city_id))->order_by('user_id','desc')->get(TBL_ESCORT)->result();
							}
						}
						$escortList = $escortList +$escortListTemp;
					}
				}
			}
			if($escortList){
				foreach ($escortList as $escort){
					$idList[] = $escort->id;
				}
			}
		}
		return array_unique($idList);
	}
	public function countUserList(){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		$ci->db->where('permission_slug', USER_TYPE_AGENT);
		return $ci->db->count_all_results(TBL_USER);
	}
	
	//end class
}